package cn.piesat.scanning.service.impl;

import cn.piesat.scanning.dao.DbDmsDatafileNearlineStorageDAO;
import cn.piesat.scanning.dto.DbDmsDatafileNearlineStorageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 近线数据服务层接口方法实现
 * @author xhl
 * @Date 2020/7/21
 */
@Service
public class DbDmsDatafileNearlineStorageServiceImpl implements cn.piesat.scanning.service.DbDmsDatafileNearlineStorageService {

    @Autowired
    private DbDmsDatafileNearlineStorageDAO dbDmsDatafileNearlineStorageDAO;

    /**
     * 添加近线数据信息至数据库
     * @param dto 近线数据实体
     */
    @Override
    public void addEntity(DbDmsDatafileNearlineStorageDTO dto) {
        try{
            dbDmsDatafileNearlineStorageDAO.save(dto);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<DbDmsDatafileNearlineStorageDTO> regexAndDateRangeTime(String date, String fileNamePattern) {
        return dbDmsDatafileNearlineStorageDAO.regexAndDateRangeFind(date,fileNamePattern);
    }

    @Override
    public void deleteOne(DbDmsDatafileNearlineStorageDTO dto) {
        dbDmsDatafileNearlineStorageDAO.delete(dto);
    }

    @Override
    public List<DbDmsDatafileNearlineStorageDTO> findMetadataFileNameIn(List<String> fileNameList) {
        if (fileNameList.isEmpty()) {
            return new ArrayList<>();
        }
        return dbDmsDatafileNearlineStorageDAO.findAllByTaskRIdIsNullAndFileNameIn(fileNameList);
    }

    @Override
    public List<DbDmsDatafileNearlineStorageDTO> findResultDataFileNameIn(List<String> fileNameList) {
        if (fileNameList.isEmpty()) {
            return new ArrayList<>();
        }
        return dbDmsDatafileNearlineStorageDAO.findAllByTaskRIdIsNotNullAndFileNameIn(fileNameList);
    }

    @Override
    public List<DbDmsDatafileNearlineStorageDTO> findBeforeDayAndLevelListData(Integer day, List<String> levelList) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        long plus = -1 * (day - 1);
        LocalDate now = LocalDate.now();
        String limitTime = dtf.format(LocalDateTime.of(now.plusDays(plus), LocalTime.MIN));
        return dbDmsDatafileNearlineStorageDAO.findAllByDataStatusAndFileDateBeforeAndDataLevelIn(0,limitTime,levelList);
    }

    @Override
    public List<DbDmsDatafileNearlineStorageDTO> findAfterDayAndLevelListData(Integer day, List<String> levelList) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        long plus = -1 * day;
        LocalDate now = LocalDate.now();
        String limitTime = dtf.format(LocalDateTime.of(now.plusDays(plus), LocalTime.MAX));
        return dbDmsDatafileNearlineStorageDAO.findAllByDataStatusAndFileDateAfterAndDataLevelIn(0,limitTime,levelList);
    }


}
